package sunger.net.org.dropmenu.adapter;

import butterknife.ButterKnife;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import sunger.net.org.dropmenu.ResourceTable;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * FilterAdapter
 */
public class FilterAdapter extends BaseItemProvider {
    private final Context context;
    private final List<String> list;
    private int checkItemPosition = 0;

    /**
     * 构造器
     *
     * @param context 上下文
     * @param strings 数据
     */
    public FilterAdapter(Context context, String[] strings) {
        this.context = context;
        this.list = Arrays.asList(strings);
    }

    /**
     * 检查选中
     *
     * @param position 位置
     */
    public void setCheckItem(int position) {
        checkItemPosition = position;
        notifyDataChanged();
    }

    /**
     * 数量
     *
     * @return 数量
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * 获取item
     *
     * @param position 位置
     * @return item
     */
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    /**
     * 获取id
     *
     * @param position 位置
     * @return id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获取子控件
     *
     * @param position 位置
     * @param component 控件
     * @param componentContainer 集合
     * @return Component
     */
    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        MyViewHolder viewHolder;
        if (component != null) {
            viewHolder = (MyViewHolder) component.getTag();
        } else {
            component = LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_item_default_drop_down, null, false);
            viewHolder = new MyViewHolder(component);
            viewHolder.mText = (Text) component.findComponentById(ResourceTable.Id_text);
            component.setTag(viewHolder);
        }
        fillValue(position, viewHolder);
        return component;
    }

    private void fillValue(int position, MyViewHolder viewHolder) {
        viewHolder.mText.setText(list.get(position));
        if (checkItemPosition != -1) {
            if (checkItemPosition == position) {
                viewHolder.mText.setTextColor(getColor(ResourceTable.Color_drop_down_selected));
                viewHolder.mText.setBackground(getShapeElement(1));
            } else {
                viewHolder.mText.setTextColor(getColor(ResourceTable.Color_drop_down_unselected));
                viewHolder.mText.setBackground(getShapeElement(0));
            }
        }
    }

    private Color getColor(int id) {
        int colorInt = 0;
        try {
            colorInt = context.getResourceManager().getElement(id).getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.getMessage();
        }
        return new Color(colorInt);
    }

    private ShapeElement getShapeElement(int id) {
        ShapeElement shapeElement = new ShapeElement();
        if (id == 1) {
            shapeElement.setRgbColor(new RgbColor(240, 240, 240));
        } else {
            shapeElement.setRgbColor(new RgbColor(	255, 255, 255));
        }
        return shapeElement;
    }

    static class MyViewHolder {
        Text mText;

        MyViewHolder(Component view) {
            ButterKnife.bind(this, view);
        }
    }
}
